﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Library
{
    public class IEServiceContainer : IServiceProvider
    {
        Dictionary<Type, object> services = new Dictionary<Type, object>();

        public void AddService(Type Service, object Provider)
        {
            if (services.ContainsKey(Service))
                throw new Exception("The service container already has this provider");
            this.services.Add(Service, Provider);
        }

        public object GetService(Type Service)
        {
            foreach (Type type in services.Keys)
                if (type == Service)
                    return services[type];

            throw new Exception("The service container does not contain a service provider of that type");
        }

        public T GetService<T>()
        {
            object res = GetService(typeof(T));
            if (res != null)
                return (T)res;
            return default(T);
        }

        public void RemoveService(Type Service)
        {
            if (services.ContainsKey(Service))
                services.Remove(Service);
        }

        public bool ContainsService(Type Service)
        {
            return services.ContainsKey(Service);
        }
    }
}